package com.gmail.dmitryustimov.ms.java7.patternslib.visitor.actionmap;

import java.util.HashMap;
import java.util.Map;

public abstract class Visitor<T> {

    private Map<Class<? extends T>, VisitAction<? extends T>> actions;

    public void init(Map<Class<? extends T>, VisitAction<? extends T>> mapVisitActions) {
        actions = new HashMap<>(mapVisitActions);
    }

    public <E extends T> void visit(E element) {
        VisitAction<E> action = getAction(((Class<E>)element.getClass()));
        if (action != null) {
            action.visit(element);
        }
    }

    private <E extends T> VisitAction<E> getAction(Class<E> clazz) {
        return (VisitAction<E>) actions.get(clazz);
    }
}